package com.qd.panda.my;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.panda.domain.entity.integral.UserIntegralDetailDTO;
import com.qd.common.panda.domain.entity.msg.UserMsgDTO;
import com.qd.common.panda.domain.entity.msg.UserMsgTypeDTO;
import com.qd.common.panda.domain.entity.user.UserBankDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.integral.UserIntegralDetailService;
import com.qd.panda.service.integral.UserIntegralService;
import com.qd.panda.service.msg.UserMsgService;
import com.qd.panda.service.user.UserBankService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * APP_我的
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/user/")
@Slf4j
public class UserMsgController {
    private final UserMsgService userMsgService;
    private final UserIntegralService userIntegralService;
    private final UserIntegralDetailService userIntegralDetailService;
    private final UserBankService userBankService;

    /**
     * 消息列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/msg/page")
    public IPage<UserMsgDTO> page(UserMsgDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        return userMsgService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 消息详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/msg/getById")
    public UserMsgDTO getById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return userMsgService.getByIdDto(id);
    }

    /**
     * 消息已读
     *
     * @param map 主键
     * @return 记录
     */
    @PostMapping(value = "/msg/read")
    public ApiResultWrapper<Object> read(@Validated @RequestBody(required = false) Map<String, String> map) {
        final String id = map.get("id");
        userMsgService.read(id);
        return ApiResult.success();
    }

    /**
     * 我的积分
     *
     * @param userId 用户ID
     * @return 积分
     */
    @GetMapping(value = "/integral/myIntegral")
    public ApiResultWrapper<Object> myIntegral(String userId) {
        if (!StringUtils.hasLength(userId)) {
            userId = SecurityUtils.getLoginUser().getUserId();
        }
//        Map<String, Object> map = new HashMap<>(5);
//        //抵扣金额
//        BigDecimal deductionAmount = new BigDecimal("0");
//        //积分
//        BigDecimal integral = userIntegralService.getIntegral(userId);
//        if (integral.compareTo(BigDecimal.ZERO) == BigDecimal.ONE.signum()) {
//            Money money = new Money(integral).multiplyBy(GasStationHolderService.DEDUCTION);
//            if (money.getAmount().compareTo(BigDecimal.ZERO) == BigDecimal.ONE.signum()) {
//                deductionAmount = money.getAmount();
//                String str = "可使用{0}熊猫豆，抵扣{1}元";
//                map.put("deductionAmountDescribe", MessageFormat.format(str, integral.stripTrailingZeros().toPlainString(), deductionAmount.stripTrailingZeros().toPlainString()));
//            }
//        }
//        map.put("integral", integral.doubleValue());
//        map.put("integralDisplay", integral.stripTrailingZeros().toPlainString());
//        map.put("deductionAmount", deductionAmount.doubleValue());
//        map.put("deductionAmountDisplay", deductionAmount.stripTrailingZeros().toPlainString());
//        return ApiResult.success(map);
        return ApiResult.success(userIntegralService.getIntegral(userId).stripTrailingZeros().toPlainString());
    }

    /**
     * 我的积分明细分页
     *
     * @param dto 查询条件
     * @return 积分
     */
    @GetMapping(value = "/integral/pageMyIntegralDetail")
    public IPage<UserIntegralDetailDTO> pageMyIntegralDetail(UserIntegralDetailDTO dto) {
        if (!StringUtils.hasLength(dto.getUserId())) {
            dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        }
        return userIntegralDetailService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 我的积分明细详情
     *
     * @param id 主键
     * @return 积分
     */
    @GetMapping(value = "/integral/myIntegralDetail")
    public UserIntegralDetailDTO myIntegralDetail(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return userIntegralDetailService.getByIdDto(id);
    }

    /**
     * 添加用户银行卡
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/userBack/save")
    public UserBankDTO save(@Validated({DefaultGroup.class}) @RequestBody UserBankDTO dto) {
        if (!StringUtils.hasLength(dto.getUserId())) {
            dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        }
        dto.setAddTime(LocalDateTime.now());
        return userBankService.save(dto);
    }

    /**
     * 修改用户银行卡
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PutMapping(value = "/userBack/update")
    public UserBankDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody UserBankDTO dto) {
        if (!StringUtils.hasLength(dto.getUserId())) {
            dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        }
        dto.setUpdateTime(LocalDateTime.now());
        return userBankService.update(dto);
    }

    /**
     * 删除用户银行卡
     *
     * @return 成功 or 失败
     */
    @DeleteMapping(value = "/userBack/delete")
    public ApiResultWrapper<Object> delete() {
        userBankService.removeById(SecurityUtils.getLoginUser().getUserId());
        return ApiResult.success("删除成功");
    }

    /**
     * 获取用户银行卡
     *
     * @return 记录
     */
    @GetMapping(value = "/userBack/getById")
    public UserBankDTO getById() {
        return userBankService.getByIdDto(SecurityUtils.getLoginUser().getUserId());
    }

    /**
     * 消息分类列表
     * @return 列表
     */
    @GetMapping(value = "/msg/msgTypeByUserId")
    public List<UserMsgTypeDTO> msgTypeByUserId(){
        return userMsgService.msgTypeByUserId(SecurityUtils.getLoginUser().getUserId());
    }
}